package com.roncoo.education.course.dao.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Repository;

import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.tools.IdWorker;
import com.roncoo.education.course.dao.ExperimentDao;
import com.roncoo.education.course.dao.impl.mapper.ExperimentMapper;
import com.roncoo.education.course.dao.impl.mapper.entity.Experiment;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentExample;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentWithBLOBs;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

/**
 * 实验信息 服务实现类
 *
 * @author wujing
 * @date 2023-07-18
 */
@Repository
@RequiredArgsConstructor
public class ExperimentDaoImpl implements ExperimentDao {

    @NotNull
    private final ExperimentMapper mapper;

    @Override
    public int save(ExperimentWithBLOBs record) {
        if (record.getId() == null) {
            record.setId(IdWorker.getId());
        }
        return this.mapper.insertSelective(record);
    }

    @Override
    public int deleteById(Long id) {
        return this.mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateById(ExperimentWithBLOBs record) {
        record.setGmtCreate(null);
        record.setGmtModified(null);
        return this.mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Experiment getById(Long id) {
        return this.mapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Experiment> page(int pageCurrent, int pageSize, ExperimentExample example) {
        int count = this.mapper.countByExample(example);
        pageSize = PageUtil.checkPageSize(pageSize);
        pageCurrent = PageUtil.checkPageCurrent(count, pageSize, pageCurrent);
        int totalPage = PageUtil.countTotalPage(count, pageSize);
        example.setLimitStart(PageUtil.countOffset(pageCurrent, pageSize));
        example.setPageSize(pageSize);
        return new Page<>(count, totalPage, pageCurrent, pageSize, this.mapper.selectByExample(example));
    }

    @Override
    public List<Experiment> listByExample(ExperimentExample example) {
        return this.mapper.selectByExample(example);
    }

    @Override
    public int countByExample(ExperimentExample example){
        return this.mapper.countByExample(example);
    }
    
    @Override
    public List<Experiment> listByIds(List<Long> experimentIds) {
        if (CollUtil.isEmpty(experimentIds)) {
            return Collections.emptyList();
        }
        ExperimentExample example = new ExperimentExample();
        example.createCriteria().andIdIn(experimentIds);
        example.setOrderByClause("field(id," + experimentIds.stream().map(String::valueOf).collect(Collectors.joining(",")) + ")");
        return this.mapper.selectByExample(example);
    }
}
